<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>部门管理</title> <!-- 修改页面标题 -->
    <!-- 引入 Bootstrap CSS -->
    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.3.7/css/bootstrap.min.css" rel="stylesheet">
    <!-- 引入 Font Awesome -->
    <link href="https://cdn.bootcdn.net/ajax/libs/font-awesome/6.5.0/css/all.min.css" rel="stylesheet">
    <!-- 引入自定义样式 -->
    <link rel="stylesheet" href="css/style.css">
    <!-- toastify.js CSS -->
    <link href="https://cdn.bootcdn.net/ajax/libs/toastify-js/1.12.0/toastify.css" rel="stylesheet">
    <!-- toastify.js JS -->
    <script src="https://cdn.bootcdn.net/ajax/libs/toastify-js/1.12.0/toastify.min.js"></script>
    <!-- Grid.js CSS -->
    <link href="https://cdn.bootcdn.net/ajax/libs/gridjs/6.2.0/theme/mermaid.min.css" rel="stylesheet" />
    <!-- Grid.js JS -->
    <script src="https://cdn.bootcdn.net/ajax/libs/gridjs/6.2.0/gridjs.umd.js"></script>
</head>
<body>
<div class="container-fluid">
    <div class="row">
        <!-- 侧边栏：高亮部门管理链接 -->
        <nav class="col-md-3 col-lg-2 d-md-block bg-light sidebar">
            <div class="position-sticky">
                <div class="sidebar-brand d-flex align-items-center px-3 py-3 border-bottom">
                    <i class="fas fa-user-cog fa-2x me-2 text-primary"></i>
                    <span class="fs-5 fw-bold">用户管理系统</span>
                </div>
                <ul class="nav flex-column pt-3">
                    <li class="nav-item pt-2">
                        <a class="nav-link" href="user.html" id="userManagementLink"> <!-- 跳转用户管理页面 -->
                            <i class="fas fa-users fa-fw me-2"></i>
                            用户管理
                        </a>
                    </li>
                    <li class="nav-item pt-2">
                        <a class="nav-link active" href="#" id="departmentManagementLink"> <!-- 高亮当前页面 -->
                            <i class="fas fa-building fa-fw me-2"></i>
                            部门管理
                        </a>
                    </li>
                    <li class="nav-item pt-2">
                        <a class="nav-link" href="role.html" id="roleManagementLink"> <!-- 跳转角色管理页面 -->
                            <i class="fas fa-user-tag fa-fw me-2"></i>
                            角色管理
                        </a>
                    </li>
                    <li class="nav-item pt-2">
                        <a class="nav-link" href="./profile.html" id="profileLink">
                            <i class="fas fa-user-circle fa-fw me-2"></i>
                            个人信息
                        </a>
                    </li>
                    <li class="nav-item pt-2">
                        <a class="nav-link" href="#" id="logoutLink" onclick="logout()">
                            <i class="fas fa-sign-out-alt fa-fw me-2"></i>
                            退出登录
                        </a>
                    </li>
                </ul>
            </div>
        </nav>

        <!-- 主内容区域：修改标题为部门管理 -->
        <main class="col-md-9 ms-sm-auto col-lg-10 px-md-4">
            <!-- ========== 页头：标题 + 操作区 ========== -->
            <div class="row mb-4">
                <div class="col-12 d-flex justify-content-between align-items-center">
                    <div>
                        <h2 class="mb-0">
                            <i class="fas fa-building text-primary me-2"></i> <!-- 更换图标 -->
                            部门管理
                        </h2>
                    </div>
                    <div>
                        <button data-bs-toggle="modal" data-bs-target="#addModal" class="btn btn-success">
                            <i class="fas fa-plus-circle me-1"></i> 新增部门
                        </button>
                        <button class="btn btn-outline-secondary ms-2" id="btnRefresh" onclick="initGrid()">
                            <i class="fas fa-sync-alt me-1"></i> 刷新
                        </button>
                    </div>
                </div>
            </div>

            <!-- ========== 部门列表卡片 ========== -->
            <div class="row">
                <div class="col-12">
                    <div class="card-header bg-white d-flex justify-content-between align-items-center border-bottom">
                        <!-- 部门搜索表单：保留部门相关搜索字段 -->
                        <form id="search-form" class="row row-cols-lg-auto g-3 align-items-center">
                            <div class="col-12">
                                <div class="input-group">
                                    <input type="text" class="form-control" name="name" placeholder="部门名称">
                                </div>
                            </div>
                            <div class="col-12">
                                <select class="form-select" name="enableState">
                                    <option disabled selected value="">状态</option>
                                    <option value="启用">启用</option>
                                    <option value="停用">停用</option>
                                </select>
                            </div>
                            <div class="col-12">
                                <button type="submit" class="btn btn-primary"><i class="fas fa-search"></i> 查询</button>
                                <button type="reset" class="btn btn-secondary"><i class="fas fa-arrow-repeat"></i> 重置</button>
                            </div>
                        </form>
                    </div>
                    <div class="card-body p-0">
                        <div class="table-responsive">
                            <!-- Grid.js 将挂载到这个 div -->
                            <div id="gridjs-container"></div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 部门表单模态框：移除用户相关字段，保留部门核心字段 -->
            <div class="modal fade modal-lg" id="addModal" tabindex="-1" aria-labelledby="addModalLabel" aria-hidden="true">
                <form id="add-form" class="needs-validation" novalidate>
                    <input type="hidden" class="form-control" name="id" />
                    <div class="modal-dialog">
                        <div class="modal-content">
                            <div class="modal-header">
                                <h5 class="modal-title" id="addModalLabel">新增部门</h5> <!-- 模态框标题 -->
                                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                            </div>
                            <div class="modal-body">
                                <div class="px-5">
                                    <div id="alertAddMessage"></div>
                                    <!-- 部门名称（必填） -->
                                    <div class="row mb-3">
                                        <label for="name" class="col-sm-2 col-form-label d-flex justify-content-end">部门名称</label>
                                        <div class="col-sm-10">
                                            <input type="text" class="form-control" id="name" name="name" required>
                                        </div>
                                    </div>
                                    <!-- 部门编码（必填） -->
                                    <div class="row mb-3">
                                        <label for="code" class="col-sm-2 col-form-label d-flex justify-content-end">部门编码</label>
                                        <div class="col-sm-10">
                                            <input type="text" class="form-control" id="code" name="code" required>
                                        </div>
                                    </div>
                                    <!-- 上级部门（下拉选择） -->
                                    <div class="row mb-3">
                                        <legend class="col-sm-2 col-form-label d-flex justify-content-end">上级部门</legend>
                                        <div class="col-sm-10">
                                            <select name="parentId" class="form-select" aria-label="select parent dept">
                                                <option value="">无（顶级部门）</option>
                                                <!-- 后续通过JS动态加载上级部门选项 -->
                                            </select>
                                        </div>
                                    </div>
                                    <!-- 部门描述 -->
                                    <div class="row mb-3">
                                        <label for="description" class="col-sm-2 col-form-label d-flex justify-content-end">部门描述</label>
                                        <div class="col-sm-10">
                                            <textarea class="form-control" id="description" name="description" rows="3" placeholder="请输入部门描述"></textarea>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-secondary" name="close" data-bs-dismiss="modal">关闭</button>
                                <button type="submit" class="btn btn-primary">提交</button>
                            </div>
                        </div>
                    </div>
                </form>
            </div>

            <div class="modal fade modal-lg" id="editModal" tabindex="-1" aria-labelledby="editModalLabel" aria-hidden="true">
                <form id="edit-form" class="needs-validation" novalidate>
                    <input type="hidden" class="form-control" name="id" />
                    <div class="modal-dialog">
                        <div class="modal-content">
                            <div class="modal-header">
                                <h5 class="modal-title" id="editModalLabel">编辑部门</h5> <!-- 模态框标题 -->
                                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                            </div>
                            <div class="modal-body">
                                <div class="px-5">
                                    <div id="alertEditMessage"></div>
                                    <!-- 部门名称（必填） -->
                                    <div class="row mb-3">
                                        <label for="e_name" class="col-sm-2 col-form-label d-flex justify-content-end">部门名称</label>
                                        <div class="col-sm-10">
                                            <input type="text" class="form-control" id="e_name" name="name" required>
                                        </div>
                                    </div>
                                    <!-- 部门编码（禁用，不可修改） -->
                                    <div class="row mb-3">
                                        <label for="e_code" class="col-sm-2 col-form-label d-flex justify-content-end">部门编码</label>
                                        <div class="col-sm-10">
                                            <input type="text" class="form-control" id="e_code" name="code" disabled> <!-- 编码禁用修改 -->
                                        </div>
                                    </div>
                                    <!-- 上级部门（下拉选择） -->
                                    <div class="row mb-3">
                                        <legend class="col-sm-2 col-form-label d-flex justify-content-end">上级部门</legend>
                                        <div class="col-sm-10">
                                            <select name="parentId" class="form-select" aria-label="select parent dept">
                                                <option value="">无（顶级部门）</option>
                                                <!-- 后续通过JS动态加载上级部门选项 -->
                                            </select>
                                        </div>
                                    </div>
                                    <!-- 部门描述 -->
                                    <div class="row mb-3">
                                        <label for="e_description" class="col-sm-2 col-form-label d-flex justify-content-end">部门描述</label>
                                        <div class="col-sm-10">
                                            <textarea class="form-control" id="e_description" name="description" rows="3" placeholder="请输入部门描述"></textarea>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-secondary" name="close" data-bs-dismiss="modal">关闭</button>
                                <button type="submit" class="btn btn-primary">提交</button>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </main>
    </div>
</div>

<!-- 引入 Bootstrap JS（含 Popper）-->
<script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.3.7/js/bootstrap.bundle.min.js"></script>
<!-- 引入部门管理专用脚本 -->
<script src="js/department.js"></script>
</body>
</html>